// socket.service.ts
import { Injectable } from '@angular/core';
import io, { Socket } from 'socket.io-client';

@Injectable({ providedIn: 'root' })
export class SocketService {
    private url = 'wss://screen.haixihudong.com?aid='; // 服务器地址
    private socket!: Socket;

    init(aid: string = 'yRX3Z0') {
        this.socket = io(`${this.url}${aid}`, {
            // 显式指定websocket传输层
            transports: ["websocket"],
            reconnectionAttempts: 3, // 失败后重新连接次数，一直失败总共尝试四次
            reconnectionDelay: 2000 //
        });
    }

    send(event: string, data: any): void {
        this.socket.emit(event, data);
    }

    on(event: string, cb: (data: any) => void): any {
        this.socket.on(event, cb);
    }
}